package com.ph.irp.base.measure.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.measure.dao.PBaseMeasureUnitDAO;
import com.ph.irp.base.measure.service.PBaseMeasureUnitService;
import com.ph.irp.base.measureunitgroup.dao.PBaseMeasureUnitGroupDAO;
import com.ph.irp.dto.MeasureDTO;
import com.ph.irp.model.PBaseMeasuregroup;
import com.ph.irp.model.PBaseMeasureunit;

public class PBaseMeasureUnitServiceImpl implements PBaseMeasureUnitService {

	private PBaseMeasureUnitDAO measureUnitDaoProxy;
	private PBaseMeasureUnitGroupDAO groupDaoProxy;
	
	public PBaseMeasureUnitDAO getMeasureUnitDaoProxy() {
		return measureUnitDaoProxy;
	}

	public void setMeasureUnitDaoProxy(PBaseMeasureUnitDAO measureUnitDaoProxy) {
		this.measureUnitDaoProxy = measureUnitDaoProxy;
	}

	public PBaseMeasureUnitGroupDAO getGroupDaoProxy() {
		return groupDaoProxy;
	}

	public void setGroupDaoProxy(PBaseMeasureUnitGroupDAO groupDaoProxy) {
		this.groupDaoProxy = groupDaoProxy;
	}
	/**
	 * 新增计量单位
	 */
	@Override
	public void addMeasureUnit(PBaseMeasureunit model) throws Throwable {

		measureUnitDaoProxy.addMeasureUnit(model);
		
	}
	/**
	 * 删除计量单位
	 */
	@Override
	public void deleteMeasureUnit(Object[] id) throws Throwable {
		
		for (int i = 0; i < id.length; i++) {
				measureUnitDaoProxy.deleteMeasureUnit(Integer.valueOf((String) id[i]));
		}
		
	}
	/**
	 * 查询所有的计量单位
	 */
	@Override
	public List<MeasureDTO> getMeasureUnitAll() throws Throwable {
		return this.convertList(measureUnitDaoProxy.getAllMeasureUnit(), new ArrayList<MeasureDTO>()) ;
	}
	/**
	 * 查询计量单位byCode
	 * 
	 */
    public List<MeasureDTO>  getMeasureUnitByCode(String code)throws Throwable{
    	
		List<MeasureDTO> measureList = new ArrayList<MeasureDTO>();
		List<PBaseMeasureunit> unitList = new ArrayList<PBaseMeasureunit>();
		
		if(!code.equals("")){
			
			unitList = this.measureUnitDaoProxy.getMeasureUnitByCode(code) ;						
		}
		
		measureList = this.convertList(unitList, measureList) ;
		return measureList;
    }
	/**
	 * 查询计量单位byName
	 * 
	 */
	public List<MeasureDTO>  getMeasureUnitByName(String name)throws Throwable{
		List<MeasureDTO> measureList = new ArrayList<MeasureDTO>();
		List<PBaseMeasureunit> unitList = new ArrayList<PBaseMeasureunit>();
		
		if(!name.equals("")){
			unitList = this.measureUnitDaoProxy.getMeasureUnitByName(name) ;
		}
		
		measureList = this.convertList(unitList, measureList) ;
		return measureList;
	}
	
	public PBaseMeasureunit  getMeasureUnitByUnitName(String name)throws Throwable{
		return measureUnitDaoProxy.getMeasureUnitByUnitName(name);
		
	}
	/**
	 * 查询计量单位byCodeAndName
	 * 
	 */
	public List<MeasureDTO>  getMeasureUnitByNameAndCode(String name,String code)throws Throwable{
		List<MeasureDTO> measureList = new ArrayList<MeasureDTO>();
		List<PBaseMeasureunit> unitList = new ArrayList<PBaseMeasureunit>();
		
		if(!code.equals("")&&!name.equals("")){			
			unitList = this.measureUnitDaoProxy.getMeasureUnitByNameAndCode(name, code);				
		}
		measureList = this.convertList(unitList, measureList) ;
		return measureList;
	}
	
	
	
	
	/**
	 * 查询计量单位byID
	 */
	@Override
	public PBaseMeasureunit getMeasureUnitById(int id) throws Throwable{
		return measureUnitDaoProxy.getMeasureUnitById(id);
	}

	public PBaseMeasureunit getDefaultMeasureUnitByGroupId(int id)throws Throwable{
		return measureUnitDaoProxy.getDefaultMeasureUnitByGroupId(id);
	}

	/**
	 * 修改计量单位
	 */
	@Override
	public void updateMeasureUnit(PBaseMeasureunit model) throws Throwable {
		measureUnitDaoProxy.updateMeasureUnit(model);
	}
	/**
	 * 查询计量单位byGroupID
	 */
	@Override
	public List<MeasureDTO> getMeasureUnitByGroupId(PBaseMeasureunit model) throws Throwable{
		List<MeasureDTO> measures = new ArrayList<MeasureDTO>();
		List<PBaseMeasureunit> list = measureUnitDaoProxy
				.getMeasureUnitByGroupId(model);
		for (PBaseMeasureunit pbmu : list) {
			MeasureDTO measure = new MeasureDTO();
			int groupId = pbmu.getMeasureGroupId();
			PBaseMeasuregroup group = groupDaoProxy.getMeasureGroupById(groupId);
			measure.setMeasuregroup(group);
			measure.setMeasureunit(pbmu);
			measures.add(measure);
		}
		return measures;
	}
	
	
	/**
	 * 根据唯一的名字得到Model
	 */
	public PBaseMeasureunit getMeasureUnitForSellPrice(String name) throws Throwable{
		return measureUnitDaoProxy.getMeasureUnitForSellPrice(name);
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 将model转化为dto
	 * @param 
	 * @param 
	 * @return
	 * @throws Throwable 
	 */
	private List<MeasureDTO> convertList(List<PBaseMeasureunit> measureunitList,List<MeasureDTO> measureunitDTOList) throws Throwable{	
		int i = 0 ;
		for(i=0;i<measureunitList.size();i++){			
			MeasureDTO measuredto = new MeasureDTO();			
			measuredto.setMeasureunit(measureunitList.get(i));			
			PBaseMeasuregroup measuregroup = groupDaoProxy.getMeasureGroupById(measureunitList.get(i).getMeasureGroupId()) ;		
			measuredto.setMeasuregroup(measuregroup);			
			measureunitDTOList.add(measuredto) ;	
		}
		
		return measureunitDTOList ;
	}
}
